import 'package:flutter/material.dart';

/// FileName third_ad
/// @Author zhuqingfang
/// @Date 2022/9/27 3:40 下午
/// @Description third ad

//广告事件回调（例如加载成功、展示等）通知给业务方
typedef ThirdAdEventCallback = void Function(
    AdCallbackEvent adCallbackCode, String message, Object? extraInfo);

//广告事件类型枚举
enum AdCallbackEvent {
  //广告展示失败
  showFail(-2),

  //广告加载失败
  loadFail(-1),

  //广告加载成功
  loadSuccess(0),

  //广告展示
  onShow(1),

  //关闭广告
  onClose(2),

  //点击广告
  onClick(3),

  //广告奖励
  onReward(4),

  //广告渲染成功（用于banner,native广告，代表获取widget对象成功，通知业务刷新UI）
  renderSuccess(5);

  final int value;

  const AdCallbackEvent(this.value);
}

//广告输入输出参数
class ThirdAdParams {
  ThirdAdParams(
      {required this.appId,
      required this.appKey,
      required this.placementId,
      required this.thirdAdEventCallback,
      this.bannerParams,
      this.nativeParams,
      this.userId,
      this.channel,
      this.subChannel,
      this.segmentId,
      this.extraInfo,
      this.factoryId,
      this.openLog});

  final String appId;
  final String appKey;
  final String placementId;

  //第三方广告额外参数设置，例如topOn的sceneId(场景id)
  Object? thirdAdExtraInputParam;

  //banner 广告参数，可空，其他类型广告可不传
  final BannerParams? bannerParams;

  //native广告参数，可空，其他类型广告可不传
  final NativeParams? nativeParams;
  final String? userId;
  final String? channel;
  final String? subChannel;
  final int? segmentId;
  final ThirdAdEventCallback thirdAdEventCallback;
  final Object? extraInfo;
  final String? factoryId;
  final bool? openLog;
}

//native输入输出参数
class NativeParams {
  NativeParams(
      {required this.parentViewAttribute,
      this.isTemplateRender,
      this.isAdaptiveHeight,
      this.adLogoAttribute,
      this.appIconAttribute,
      this.mainTitleAttribute,
      this.ctaAttribute,
      this.descAttribute,
      this.dislikeAttribute,
      this.mainImageAttribute});

  //是否模版渲染，默认为自渲染
  bool? isTemplateRender = false;

  //是否模版自适应（加载时自适应高度仅针对Android的穿山甲、优量汇平台，自适应高度不受parent的高度控制）
  bool? isAdaptiveHeight = false;

  //控制的是Native的总体大小
  final NativeSubViewAttribute parentViewAttribute;

  //appIcon属性控制广告的图标属性
  final NativeSubViewAttribute? appIconAttribute;

  //控制广告标题
  final NativeSubViewAttribute? mainTitleAttribute;

  //控制广告描述文字
  final NativeSubViewAttribute? descAttribute;

  //控制点击按钮
  final NativeSubViewAttribute? ctaAttribute;

  //mainImage控制广告的封面图
  final NativeSubViewAttribute? mainImageAttribute;

  //控制广告标识属性
  final NativeSubViewAttribute? adLogoAttribute;

  //关闭按钮
  final NativeSubViewAttribute? dislikeAttribute;

  //输出native widget给业务方使用
  Widget? thirdNativeAdOutputWidget;
}

//native view属性
class NativeSubViewAttribute {
  NativeSubViewAttribute(
      {required this.width,
      required this.height,
      this.x,
      this.y,
      this.backgroundColorStr,
      this.textColorStr,
      this.textSize,
      this.isCustomClick});

  final double width;
  final double height;

  //横坐标位置（以左上角为原点）
  double? x = 0;

  //纵坐标位置（以左上角为原点
  double? y = 0;

  //背景颜色
  String? backgroundColorStr = '#FFFFFF';

  //字体颜色（只针对有Text的View有效）
  String? textColorStr = '#000000';

  //字体大小（只针对有Text的View有效）
  double? textSize = 15;

  //是否点击时直接下载（只针对Android且只针对腾讯广告平台的应用类广告有效）
  bool? isCustomClick = false;
}

//banner广告 输入输出参数
class BannerParams {
  BannerParams(
      {required this.width, required this.height, this.adaptiveOrientation});

  //banner广告 宽
  final double width;

  //banner广告 高
  final double height;

  //banner自适应 只有admob支持 所以可空
  final AdaptiveOrientation? adaptiveOrientation;

  //输出banner widget给业务方使用
  Widget? thirdBannerAdOutputWidget;
}

//banner广告自适应,针对admob广告
enum AdaptiveOrientation {
  //自适应
  current(0),

  //竖屏
  portrait(1),

  //横屏
  landscape(2);

  final int value;

  const AdaptiveOrientation(this.value);
}

//抽象化的广告实例
abstract class ThirdAd {
  ThirdAd({required this.thirdAdParams});

  final ThirdAdParams thirdAdParams;

  void load();

  void show();
}
